package com.group02.community.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.owner.ZyOwner;
import com.group02.community.entity.owner.ZyOwnerRoom;
import com.group02.community.entity.owner.ZyOwnerRoomRecord;
import com.group02.community.entity.vo.OwnerVO;
import com.group02.community.mapper.OwnerMapper;
import com.group02.community.mapper.OwnerRoomMapper;
import com.group02.community.mapper.OwnerRoomRecordMapper;
import com.group02.community.service.OwnerService;
import com.group02.community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, ZyOwner> implements OwnerService {

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private OwnerRoomMapper ownerRoomMapper;

    @Autowired
    private OwnerRoomRecordMapper ownerRoomRecordMapper;

    /**
     * 获取业主的所有信息（包括小区名称，楼栋名称，单元名称，房间名称）
     * @return
     */
    @Override
    public List<ZyOwner> getAllOwner() {
        return ownerMapper.getAllOwner();
    }

    /**
     * 分页查询
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<ZyOwner> getOwnerPage(Long current, Long size, OwnerVO ownerVO) {
        Page<ZyOwner> zyOwnerPage = new Page<>(current, size);
        zyOwnerPage.setOptimizeCountSql(false);
        return ownerMapper.selectOwnerPage(zyOwnerPage ,ownerVO);
    }

    /**
     * 1.解绑房屋将房屋的room_status置为Unbind
     *2.向records中添加一个记录
     * @param ownerRoomId
     */
    @Override
    public void updateRoomById(String ownerRoomId) {
        ZyOwnerRoom byOwnerRoomId = ownerRoomMapper.getByOwnerRoomId(ownerRoomId);
        ZyOwnerRoomRecord zyOwnerRoomRecord = new ZyOwnerRoomRecord();
        zyOwnerRoomRecord.setRoomId(byOwnerRoomId.getRoomId());
        zyOwnerRoomRecord.setRecordId(UUIDUtils.getId());
        zyOwnerRoomRecord.setOwnerId(byOwnerRoomId.getOwnerId());
        zyOwnerRoomRecord.setCreateTime(LocalDateTime.now());
        zyOwnerRoomRecord.setRoomStatus(byOwnerRoomId.getRoomStatus());
        zyOwnerRoomRecord.setUnitId(byOwnerRoomId.getUnitId());
        zyOwnerRoomRecord.setCommunityId(byOwnerRoomId.getCommunityId());
        zyOwnerRoomRecord.setBuildingId(byOwnerRoomId.getBuildingId());
        zyOwnerRoomRecord.setOwnerRoomId(String.valueOf(byOwnerRoomId.getOwnerRoomId()));
        zyOwnerRoomRecord.setOwnerType(byOwnerRoomId.getOwnerType());
        System.out.println(zyOwnerRoomRecord);
        ownerRoomRecordMapper.insert(zyOwnerRoomRecord);
        ownerRoomMapper.updateOwnerId(ownerRoomId);
    }


    @Override
    public void uploadExcel(HttpServletResponse response) throws IOException {
        //查询所有数据
        List<ZyOwner> allOwner = ownerMapper.getAllOwner();
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(), ZyOwner.class).sheet(System.currentTimeMillis()+"").doWrite(allOwner);
    }

    @Override
    public ZyOwner getOwnerInfo(String phoneNumber, String password) {
        ZyOwner zyOwner = ownerMapper.getOwnerInfo(phoneNumber, password);
        return zyOwner;
    }
}
